package Bean;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import sqlconifg.JavaConfig;
import sqlconifg.MysqlSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

@Bean
public class Pool {
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    String DB_URL = "jdbc:mysql://localhost:3306/shujuku_work";
    static final String USER = "root";
    static final String PASS = "kong";
    Connection con;
    Statement st;

    Pool() throws Throwable {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("加载失败！");
            e.printStackTrace();
        }
        con = DriverManager.getConnection(DB_URL, USER, PASS);
        st = con.createStatement();
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfig.class);
        MysqlSource mysqlSource = ctx.getBean("mysqlSource", MysqlSource.class);
        con = mysqlSource.getCon();
        st = mysqlSource.getSt();
    }

    ResultSet getSetFromat(String tableName, String xinxi, String x) throws Throwable {
        String order = "SELECT * FROM %s WHERE %s = '%s'";

        System.out.println(String.format(order, tableName, xinxi, x));
        return st.executeQuery(String.format(order, tableName, xinxi, x));
    }

    ResultSet getSetFromat3(String tableName, String col1, String val1, String col2, String val2) throws Throwable {
        String order = "SELECT * FROM %s WHERE %s = '%s' and %s %s ";

        System.out.println(String.format(order, tableName, col1, val1, col2, val2));
        return st.executeQuery(String.format(order, tableName, col1, val1, col2, val2));
    }

    ResultSet getSetFromat2(String tableName, String col1, String val1, String col2, String val2, String col3, String val3) throws Throwable {
        String order = "SELECT * FROM %s WHERE %s = '%s' and %s = '%s' and %s = '%s'";

        System.out.println(String.format(order, tableName, col1, val1, col2, val2, col3, val3));
        return st.executeQuery(String.format(order, tableName, col1, val1, col2, val2, col3, val3));
    }
}